package com.suyang.syt.controller;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.auth.AuthMethod;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.SystemNavigationDTO;
import com.suyang.syt.service.interfaces.SystemNavigationService;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.JsonUtil;
import com.suyang.syt.util.StringUtil;

@SuppressWarnings("unchecked")
@Controller
@RequestMapping("/admin/systemNavigation")
@AuthClass("login")
public class AdminSystemNavigationController {
	@Autowired
	private SystemNavigationService systemNavigationService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "systemNavigation/list";
	}

	@RequestMapping(value = "/listTreegrid", method = RequestMethod.POST)
	@ResponseBody
	public List<SystemNavigationDTO> listTreegrid(HttpServletRequest request, String id) {
		try {
			long navId = 0;
			if (EmptyUtil.isNotEmpty(id)) {
				navId = Long.parseLong(id);
			}
			return systemNavigationService.listTreegrid(navId);
		} catch (Exception e) {
			return null;
		}

	}

	/** 根据权限标志加载导航菜单 */
	private AjaxObj listNav(HttpServletRequest request, String flag) {
		try {
			ServletContext application = request.getSession().getServletContext();
			List<SystemNavigationDTO> list = (List<SystemNavigationDTO>) application.getAttribute(flag);
			if (list == null) {
				list = systemNavigationService.list(flag);
				application.setAttribute(flag, JsonUtil.toJson(list));
			}
			return new AjaxObj(1, "菜单加载完毕！", list);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/listNavAdmin", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj listNavAdmin(HttpServletRequest request) {
		return listNav(request, "listNavAdmin");
	}

	@RequestMapping(value = "/listNavPXJG", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_PXJG")
	@ResponseBody
	public AjaxObj listNavManager(HttpServletRequest request) {
		return listNav(request, "listNavPXJG");
	}

	@RequestMapping(value = "/listNavJGJS", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_JGJS")
	@ResponseBody
	public AjaxObj listNavJXJY(HttpServletRequest request) {
		return listNav(request, "listNavJGJS");
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, SystemNavigationDTO systemNavigationDTO) {
		try {
			AjaxObj ajaxObj = systemNavigationService.add(systemNavigationDTO);
			if (ajaxObj != null && ajaxObj.getResult() == 1) {
				clearNav(request);
			}
			return ajaxObj;
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj delete(HttpServletRequest request, String ids) {
		try {
			AjaxObj ajaxObj = systemNavigationService.delete(ids);
			if (ajaxObj != null && ajaxObj.getResult() == 1) {
				clearNav(request);
			}
			return ajaxObj;
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, SystemNavigationDTO systemNavigationDTO) {
		try {
			AjaxObj ajaxObj = systemNavigationService.update(systemNavigationDTO);
			if (ajaxObj != null && ajaxObj.getResult() == 1) {
				clearNav(request);
			}
			return ajaxObj;
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	private void clearNav(HttpServletRequest request) {
		ServletContext servletContext = request.getSession().getServletContext();
		servletContext.setAttribute("listNavPXJG", null);
		servletContext.setAttribute("listNavPXJG", null);
		servletContext.setAttribute("listNavJGJS", null);
	}
}